<script language="javascript" type="text/javascript">
var account_id="";
function account_a()
{
	$("#faccount_add").dialog("option","disabled",true);
	$.ajax({
		url:"modules/account/a.php",
		type:"POST",
		dataType:"xml",
		data:"un="+ encodeURI($("#account_a_table #un").val())+"&pwd="+encodeURI($("#account_a_table #pwd").val())+"&magrp="+encodeURI($("#account_a_table #magrp1").val()),
		success:function(xml){
			var status=$(xml).find("status").text();
			var message=$(xml).find("message").text();
			 msgbox(status,message);
		
			if(status=="ok") 
			{
				$("#faccount_add").dialog("close");
				account_id=$(xml).find("id").text();
			}
			else $("#faccount_add").dialog("option","disabled",false);
		}
	});
}
//-----------------------------------------------------------------------------------------
function account_group_remove()
{
	$("#account_a_table #magrp").setValue("");
	$("#account_a_table #magrp1").attr("value","");
}	
//-----------------------------------------------------------------------------------------
function account_af()
{
	var faccountoptions={
		width:420,
		height:350,
		title:"Thêm người dùng",
		modal:true,
		buttons:{
			"Xóa nhóm":account_group_remove,
			"Thêm":account_a,
			"Hủy":function(){$("#faccount_add").dialog("close");}
		},
		disabled:true,
		focus:account_af_ah,
		open:rebuildACL,
		close:function(){
			$("#faccount_add").remove();
			$("#account_flex").flexReload();
			account_af_dh();
		}
	};
	
	$("body").append("<div id=\"faccount_add\" style=\"display:none;\"></div>");

	$("#faccount_add").dialog(faccountoptions);
	$("#faccount_add").load("modules/account/af.php","",function(){
		$("#faccount_add").dialog("option","disabled",false);
		$("#faccount_add").dialog("option","position","center");
	});
}
//-----------------------------------------------------------------------------------------
function account_u()
{
	$("#faccount_update").dialog("option","disabled",true);
	$.ajax({
		url:"modules/account/u.php",
		type:"POST",
		dataType:"xml",
		data:'un='+$('#account_u_table #un').val()+'&pwd='+encodeURI($('#account_u_table #pwd').val())+'&macu='+$('#account_u_table #macu').val()+"&magrp="+encodeURI($("#account_u_table #magrp1").val()),
		success:function(xml){
			var status=$(xml).find("status").text();
			var message=$(xml).find("message").text();
			msgbox(status,message);
			if(status=="ok") 
			{
				account_id=$("#account_u_table #un").val();
				$("#faccount_update").dialog("close");
			}
			else $("#faccount_update").dialog("option","disabled",false);
		}
	});
}
//-----------------------------------------------------------------------------------------
function account_uf()
{
	if(!$("#account_flex .trSelected").attr("id"))
	{
		msgbox("fail","Phải chọn một mục để cập nhật !");
		return;
	}
	var sma=$("#account_flex .trSelected").attr("id");
	sma = sma.substring(sma.lastIndexOf("row")+3);
	
	$.ajax({
		url:"modules/account/s.php",
		type:"POST",
		dataType:"xml",
		data:"un="+sma,
		success:function(xml){
			
			var k=$(xml).find("row");
			//alert(k.text());
			var faccountoptions={
						width:520,
						height:350,
						title:"Cập nhật người dùng",
						modal:true,
						buttons:{
							"Xóa nhóm":account_group_remove,
							"Cập nhật":account_u,
							"Hủy":function(){$("#faccount_update").dialog("close");}
						},
						disabled:true,
						focus:account_uf_ah,
						open:rebuildACL,
						close:function(){
							$("#faccount_update").remove();
							$("#account_flex").flexReload();
							account_uf_dh();
						}
					};

					$("body").append("<div id=\"faccount_update\" style=\"display:none;\"></div>");
					$("#faccount_update").dialog(faccountoptions);	
					$("#faccount_update").load("modules/account/uf.php","",function(){
						$("#account_u_table #un").attr("value",sma);
						
						if(k[0].childNodes[1].text) $("#account_u_table #pwd").attr("value",k[0].childNodes[1].text);
						else $("#account_u_table #pwd").attr("value",k[0].childNodes[1].textContent);

						if(k[0].childNodes[2].text) 
						{
							$("#account_u_table #magrp").setValue(k[0].childNodes[3].text);
							$("#account_u_table #magrp1").attr("value",k[0].childNodes[2].text);
						}
						else 
						{
							$("#account_u_table #magrp").setValue(k[0].childNodes[3].textContent);
							$("#account_u_table #magrp1").attr("value",k[0].childNodes[2].textContent);
						}

						$("#account_u_table #macu").attr("value",sma);
						$("#faccount_update").dialog("option","disabled",false);
						$("#faccount_update").dialog("option","position","center");
					});
				}
	});
}
//-----------------------------------------------------------------------------------------
function account_df()
{
	if(!$("#account_flex .trSelected").attr("id"))
	{
		msgbox("fail","Phải chọn một mục để xoá !");
		return;
	}
	var sma=$("#account_flex .trSelected").attr("id");
	sma = sma.substring(sma.lastIndexOf("row")+3);
	$.confirmDialog({
			msg:'Có chắc chắn xoá  người dùng '+sma+' không ?',
			yes:"Có",
			no:"Không",
title:"Xác nhận xoá"
		},
		function(){
			$("#faccount").dialog("option","disabled",true);
			$.ajax({
				url:"modules/account/d.php",
				type:"POST",
				dataType:"xml",
				data:'un='+sma,
				success:function(xml){
					var status=$(xml).find("status").text();
					var message=$(xml).find("message").text();
					
					msgbox(status,message);
					if(status=="ok") $("#account_flex").flexReload();
					$("#faccount").dialog("option","disabled",false);
					account_ah();
				}
			});		
		},function(){
			account_ah();
		});
}
/*--------------------------------------------------------------------------------------*/
function account_af_hk(e)
{
	var code=(e.keyCode ? e.keyCode : e.which);
	if(code==13)
	{
		$("#faccount_add").dialog("option","buttons")["Thêm"]();
	}
	else if(code==27)
	{
		$("#faccount_add").dialog("close");
	}
	e.stopPropagation();
	e.preventDefault();
}
/*--------------------------------------------------------------------------------------*/
function account_uf_hk(e)
{
	var code=(e.keyCode ? e.keyCode : e.which);
	if(code==13)
	{
		$("#faccount_update").dialog("option","buttons")["Cập nhật"]();
	}
	else if(code==27)
	{
		$("#faccount_update").dialog("close");
	}
	e.stopPropagation();
	e.preventDefault();
}
/*--------------------------------------------------------------------------------------*/
function account_af_ah()
{
	$(document).off("keydown");
	$(document).on("keydown",account_af_hk);
	if(event!=null) if(typeof(event)!="undefined")
	{
		event.stopPropagation();
		event.preventDefault();
	}
}
/*--------------------------------------------------------------------------------------*/
function account_af_dh()
{
	$(document).off("keydown");
	$(document).on("keydown",account_hk);
}
/*--------------------------------------------------------------------------------------*/
function account_uf_ah()
{
	$(document).off("keydown");
	$(document).on("keydown",account_uf_hk);
	if(event!=null) if(typeof(event)!="undefined")
	{
		event.stopPropagation();
		event.preventDefault();
	}
}
/*--------------------------------------------------------------------------------------*/
function account_uf_dh()
{
	$(document).off("keydown");
	$(document).on("keydown",account_hk);
}
/*--------------------------------------------------------------------------------------*/
function account_hk(e)
{ 
	var code=(e.keyCode ? e.keyCode : e.which);
	//Down
	if(code==38)
	{
		if($("#account_flex tr.trSelected").is("#account_flex tr:first")) return;
		var id=$("#account_flex tr.trSelected").prev().attr("id");
		$("#account_flex tr.trSelected").removeClass("trSelected");
		$("#account_flex #"+id).addClass("trSelected");
	}
	// Up
	else if(code==40)
	{
		if($("#account_flex tr.trSelected").is("#account_flex tr:last")) return;
		var id=$("#account_flex tr.trSelected").next().attr("id");
		$("#account_flex tr.trSelected").removeClass("trSelected");
		$("#account_flex #"+id).addClass("trSelected");
	}
	//Enter
	else if(code==13)
	{
		$("#faccount").dialog("option", "buttons")["Sửa"]();
	}
	// Xóa
	else if(code==46)
	{
		$("#faccount").dialog("option", "buttons")["Xoá"]();
	}
	// +
	else if(code==107)
	{
		$("#faccount").dialog("option", "buttons")["Thêm"]();
	}
	e.stopPropagation();
	e.preventDefault();
}
/*--------------------------------------------------------------------------------------*/
function account_ah(e)
{
	$(document).off("keydown");
	$(document).on("keydown",account_hk);
	if(event!=null) if(typeof(event)!="undefined")
	{
		event.stopPropagation();
		event.preventDefault();
	}
}
/*--------------------------------------------------------------------------------------*/
function account_dh()
{
	$(document).off("keydown");
}
/*-----------------------------------------------------------------------------------------*/
function account()
{
	if($("#faccount").is(":data(dialog)")) return;
	$("body").append("<div id=\"faccount\"></div>");
	var faccountoptions={
		width:520,
		title:"Danh mục người dùng",
		buttons:{
			"Quyền hạn":quyen,
			"Thêm":account_af,
			"Sửa":account_uf,
			"Xoá":account_df
		},
		disabled:true,
		focus:account_ah,
		close:function(){
			$("#faccount").remove();
			account_dh();
		}
	};
	$("#faccount").dialog(faccountoptions);
	$("#faccount").load("modules/account/g.php","",function(){
		$("#faccount").dialog("option","disabled",false);
	});
}
</script>
